﻿
Imports std
Imports centrosphere.job.storage
Imports command = centrosphere.job.storage.command

Partial Public Class storageJob
    Private Shared ReadOnly key_required As _do(Of storageJob, Boolean) =
        Function(ByRef i As storageJob) As Boolean
            Return Not isemptystring(i.key)
        End Function

    Private Shared ReadOnly buff_required As _do(Of storageJob, Boolean) =
        Function(ByRef i As storageJob) As Boolean
            Return Not i.buff Is Nothing
        End Function

    Private Shared ReadOnly i64_required As _do(Of storageJob, Boolean) =
        Function(ByRef i As storageJob) As Boolean
            Return i.i64 > 0
        End Function

    Public Shared ReadOnly command_required() As vector(Of _do(Of storageJob, Boolean)) = Nothing
    Private Shared Sub initial_command_required()
        traversal(Sub(ByRef name As String, ByRef cmd As command, ByRef index As Int64)
                      command_required(index) = New vector(Of _do(Of storageJob, Boolean))()
                      If keyrequired(cmd) Then
                          command_required(index).push_back(key_required)
                      End If
                      If i64required(cmd) Then
                          command_required(index).push_back(i64_required)
                      End If
                      If buffrequired(cmd) Then
                          command_required(index).push_back(buff_required)
                      End If
                  End Sub)
    End Sub

    Public Shared Function required(ByVal command_required() As vector(Of _do(Of storageJob, Boolean)),
                                    ByVal j As storageJob) As Boolean
        assert(Not command_required Is Nothing)
        assert(Not j Is Nothing)
        Dim cr As vector(Of _do(Of storageJob, Boolean)) = Nothing
        cr = command_required(j.command - job.firstOfStorage)
        For i As Int64 = 0 To cr.size() - 1
            If Not cr(i)(j) Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Shared Function required(ByVal j As storageJob) As Boolean
        Return required(command_required, j)
    End Function

    Private Shared ReadOnly result_init As void(Of storageJob) =
        Sub(ByRef i As storageJob)
            i.result = True
        End Sub

    Private Shared ReadOnly key_init As void(Of storageJob) =
        Sub(ByRef i As storageJob)
            i.key = Nothing
        End Sub

    Private Shared ReadOnly buff_init As void(Of storageJob) =
        Sub(ByRef i As storageJob)
            i.buff = Nothing
        End Sub

    Private Shared ReadOnly vs_init As void(Of storageJob) =
        Sub(ByRef i As storageJob)
            If i.vs Is Nothing Then
                i.vs = New bytesVector(Of String, bytesString)()
            Else
                i.vs.clear()
            End If
        End Sub

    Private Shared ReadOnly i64_init As void(Of storageJob) =
        Sub(ByRef i As storageJob)
            i.i64 = 0
        End Sub

    Private Shared ReadOnly i64_2_init As void(Of storageJob) =
        Sub(ByRef i As storageJob)
            i.i64_2 = 0
        End Sub

    Public Shared ReadOnly command_init() As vector(Of void(Of storageJob)) = Nothing
    Private Shared Sub initial_command_init()
        traversal(Sub(ByRef name As String, ByRef cmd As command, ByRef index As Int64)
                      command_init(index) = New vector(Of void(Of storageJob))()
                      If resultrespond(cmd) Then
                          command_init(index).push_back(result_init)
                      End If
                      If keyrespond(cmd) Then
                          command_init(index).push_back(key_init)
                      End If
                      If buffrespond(cmd) Then
                          command_init(index).push_back(buff_init)
                      End If
                      If vsrespond(cmd) Then
                          command_init(index).push_back(vs_init)
                      End If
                      If i64respond(cmd) Then
                          command_init(index).push_back(i64_init)
                      End If
                      If i64_2respond(cmd) Then
                          command_init(index).push_back(i64_2_init)
                      End If
                  End Sub)
    End Sub

    Public Shared Sub init(ByVal j As storageJob)
        assert(Not j Is Nothing)
        Dim i As vector(Of void(Of storageJob)) = Nothing
        i = command_init(j.command - job.firstOfStorage)
        For k As Int64 = 0 To i.size() - 1
            i(k)(j)
        Next
    End Sub

    Public Shared Sub result_failed_init(ByVal j As storageJob)
        assert(Not j Is Nothing)
        j.result = False
    End Sub

    Public Shared Function required_init(ByVal command_required() As vector(Of _do(Of storageJob, Boolean)),
                                         ByVal j As storageJob) As Boolean
        If required(command_required, j) Then
            init(j)
            Return True
        Else
            result_failed_init(j)
            Return False
        End If
    End Function

    Public Shared Function required_init(ByVal j As storageJob) As Boolean
        Return required_init(command_required, j)
    End Function
End Class
